package com.blockchain.console.servlets.financial.bzc;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.XlbZcStatus;
import com.blockchain.console.service.financial.BManage;
import com.blockchain.console.servlets.financial.AbstractFinancialServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;

@Right(id = "C_FINANCIAL_XLBZCPL", name = "虚拟币转出-未审核-批量审核")
public class ZbshPl extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(final HttpServletRequest request, final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		BManage manage = serviceSession.getService(BManage.class);
		String s = request.getParameter("ids");
		int[] ids = new int[0];
		if (!StringHelper.isEmpty(s)) {
			ids = IntegerParser.parseArray(s.split(","));
		}
		String bz = request.getParameter("check_reason");
		XlbZcStatus status = EnumParser.parse(XlbZcStatus.class, request.getParameter("status"));
		manage.updateBzcPl(status, bz, ids);
		if (status == XlbZcStatus.SHSB) {
			sendRedirect(request, response, getController().getURI(request, BzcList.class) + "?zczt=" + status.name());

		} else if (status == XlbZcStatus.SHTG) {
			sendRedirect(request, response, getController().getURI(request, BzcList.class) + "?zczt=" + status.name());

		}
	}

	@Override
	protected void onThrowable(HttpServletRequest request, HttpServletResponse response, Throwable throwable)
			throws ServletException, IOException {
		XlbZcStatus status = EnumParser.parse(XlbZcStatus.class, request.getParameter("zczt"));
		if (throwable instanceof ParameterException || throwable instanceof LogicalException) {
			getController().prompt(request, response, PromptLevel.WARRING, throwable.getMessage());
			sendRedirect(request, response, getController().getURI(request, BzcList.class) + "?zczt=" + status.name());
		} else {
			super.onThrowable(request, response, throwable);
		}
	}
}
